/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.ad.feedsreader.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.struts2.interceptor.SessionAware;

import ua.ad.feedsreader.mybatis.config.MyBatisSqlSessionFactory;
import ua.ad.feedsreader.mybatis.mappers.beans.SharedItemFeed;
import ua.ad.feedsreader.mybatis.mappers.beans.Utilizador;
import ua.ad.feedsreader.mybatis.mappers.beans.UtilizadorOauth;
import ua.ad.feedsreader.mybatis.mappers.clients.SharedItemFeedMapper;
import ua.ad.feedsreader.mybatis.mappers.clients.UtilizadorOauthMapper;

import com.opensymphony.xwork2.ActionSupport;
import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.types.User;

/**
 *
 * @author sexta13
 */
public class AdicionarUtilizadorFb extends ActionSupport implements SessionAware {

    /**
	 * 
	 */
	private static final long serialVersionUID = 8405422915760469941L;
	private Map<String, Object> session;
    private String chave;
    private List<SharedItemFeed> selectSharedItems;

    @Override
    public String execute() throws Exception {
        Utilizador userSession = (Utilizador) getSession().get("utilizador");

        SqlSession bdSession = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession();

        UtilizadorOauthMapper utOauthMapper = bdSession.getMapper(UtilizadorOauthMapper.class);

        List<UtilizadorOauth> lstUtOauth = utOauthMapper.selectByUserId(userSession.getId());   
        UtilizadorOauth utOauth=null;
        
        for(UtilizadorOauth ut : lstUtOauth)
        {
            if(ut.getIdTypeExtApp()==1)
                utOauth=ut;
        }
        
        if (utOauth == null) {

            FacebookClient facebookClient = new DefaultFacebookClient(this.chave);
            User userFacebook = facebookClient.fetchObject("me", User.class);

            utOauth = new UtilizadorOauth();
            utOauth.setIdTypeExtApp(1);
            utOauth.setIdUtilizador(userSession.getId());
            utOauth.setOauthCode(this.chave);
            utOauth.setIdUserExternalApp(userFacebook.getId());

            utOauthMapper.insertSelective(utOauth);
            
        } else {
        	
            utOauth.setOauthCode(chave);
            utOauthMapper.updateByPrimaryKeySelective(utOauth);
            
            String chave = (String) getSession().get("chaveFb");
        	FacebookClient facebookClient = new DefaultFacebookClient( chave );
        	
        	Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);
        	List<String> friendsId = new ArrayList<String>();
        	
        	for (List<User> list : myFriends) {
        		for (User user : list) {
//        			System.out.println( user.getName() + user.getId() );
        			friendsId.add( user.getId() );
				}
			}
        	
        	SharedItemFeedMapper mapper = bdSession.getMapper(SharedItemFeedMapper.class);
        	
        	selectSharedItems = mapper.selectSharedItems( friendsId );
        }
        
        getSession().put("chaveFb", chave);
        

        bdSession.close();
        
        return SUCCESS;

    }

    public Map<String, Object> getSession() {
        return session;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    public String getChave() {
        return chave;
    }

    public void setChave(String chave) {
        this.chave = chave;
    }

	/**
	 * @return the selectSharedItems
	 */
	public List<SharedItemFeed> getSelectSharedItems() {
		return selectSharedItems;
	}

	/**
	 * @param selectSharedItems the selectSharedItems to set
	 */
	public void setSelectSharedItems(List<SharedItemFeed> selectSharedItems) {
		this.selectSharedItems = selectSharedItems;
	}
}
